package bb.lanxing.lib.devices.core.sync;

import androidx.collection.LongSparseArray;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public abstract class AbstractSyncManager implements SyncManager {
    private static long seq;
    private final LinkedList<DeviceFile> deviceFileQueue = new LinkedList<>();
    private List<DeviceFile> deviceFiles;
    private LongSparseArray<DeviceFile> deviceFilesMap;
    private boolean isSynchronisingWithMultiFiles;
    protected SyncStatusNotifier notifier;
    private Object object;
    private LongSparseArray<Integer> stateMap;
    private LinkedList<DeviceFile> tempDeviceFileQueue;

    @Override
    public void deleteAll() {
    }

    protected abstract boolean onSync(DeviceFile deviceFile);

    public int getItemCount() {
        return this.deviceFileQueue.size();
    }

    public boolean enqueue(DeviceFile deviceFile) {
        if (deviceFile == null) {
            return false;
        }
        if (this.deviceFileQueue.contains(deviceFile)) {
            return true;
        }
        notifySyncStatus(deviceFile, DeviceFileStatus.STATUS_SYNC_PENDING.getStatus());
        this.deviceFileQueue.addLast(deviceFile);
        return true;
    }

    public boolean next() {
        if (getItemCount() <= 0) {
            return false;
        }
        return onSync(this.deviceFileQueue.removeFirst());
    }

    public DeviceFile pop() {
        if (getItemCount() <= 0) {
            return null;
        }
        DeviceFile removeFirst = this.deviceFileQueue.removeFirst();
        return !onSync(removeFirst) ? pop() : removeFirst;
    }

    public void clearPendingFiles() {
        this.deviceFileQueue.clear();
    }

    @Override
    public final boolean sync(DeviceFile deviceFile) {
        if (deviceFile == null || isSynchronised(deviceFile.getId()) || isSynchronising(deviceFile.getId())) {
            return false;
        }
        boolean isSynchronising = isSynchronising();
        enqueue(deviceFile);
        if (isSynchronising) {
            return true;
        }
        next();
        return true;
    }

    @Override
    public final boolean sync(long j) {
        DeviceFile deviceFileById = getDeviceFileById(j);
        if (deviceFileById == null) {
            return false;
        }
        return sync(deviceFileById);
    }

    @Override
    public final boolean sync() {
        List<DeviceFile> deviceFiles;
        if (!isSynchronising() && (deviceFiles = getDeviceFiles()) != null && !deviceFiles.isEmpty()) {
            for (DeviceFile deviceFile : deviceFiles) {
                if (!isSynchronised(deviceFile.getId())) {
                    enqueue(deviceFile);
                }
            }
            this.isSynchronisingWithMultiFiles = true;
            next();
            return true;
        }
        return false;
    }

    @Override
    public String getPath(long j) {
        DeviceFile deviceFileById = getDeviceFileById(j);
        if (deviceFileById != null) {
            return deviceFileById.getPath();
        }
        return null;
    }

    protected boolean isSynchronising(long j) {
        return DeviceFileStatus.valueOf(getSyncState(j)) == DeviceFileStatus.STATUS_SYNCING;
    }

    @Override
    public boolean isSynchronising() {
        return getItemCount() > 0;
    }

    @Override
    public boolean isSynchronisingWithMultiFiles() {
        return this.isSynchronisingWithMultiFiles;
    }

    public DeviceFile getDeviceFileById(long j) {
        LongSparseArray<DeviceFile> longSparseArray = this.deviceFilesMap;
        if (longSparseArray != null) {
            return longSparseArray.get(j);
        }
        return null;
    }

    public void removeById(long j) {
        DeviceFile deviceFileById = getDeviceFileById(j);
        if (deviceFileById == null) {
            return;
        }
        LongSparseArray<DeviceFile> longSparseArray = this.deviceFilesMap;
        if (longSparseArray != null) {
            longSparseArray.remove(j);
        }
        List<DeviceFile> list = this.deviceFiles;
        if (list == null) {
            return;
        }
        list.remove(deviceFileById);
    }

    public DeviceFile getDeviceFileByName(String str) {
        if (str != null && !"".equals(str) && this.deviceFiles != null) {
            for (int i = 0; i < this.deviceFiles.size(); i++) {
                DeviceFile deviceFile = this.deviceFiles.get(i);
                if (deviceFile != null && str.equals(deviceFile.getName())) {
                    return deviceFile;
                }
            }
        }
        return null;
    }

    public List<DeviceFile> getDeviceFiles() {
        return this.deviceFiles;
    }

    public void setDeviceFiles(List<DeviceFile> list) {
        this.deviceFiles = list != null ? new ArrayList(list) : null;
        this.deviceFilesMap = new LongSparseArray<>();
        List<DeviceFile> list2 = this.deviceFiles;
        if (list2 != null) {
            for (DeviceFile deviceFile : list2) {
                this.deviceFilesMap.put(deviceFile.getId(), deviceFile);
            }
        }
    }

    @Override
    public boolean isSynchronised(long j) {
        String path;
        DeviceFile deviceFileById = getDeviceFileById(j);
        if (deviceFileById == null || (path = getPath(deviceFileById.getId())) == null) {
            return false;
        }
        return new File(path).exists();
    }

    public void notifySyncStatus(DeviceFile deviceFile, int i, String str, boolean z) {
        if (deviceFile == null) {
            return;
        }
        if (z) {
            updateStatus(deviceFile.getId(), i);
        }
        SyncStatusNotifier syncStatusNotifier = this.notifier;
        if (syncStatusNotifier == null) {
            return;
        }
        syncStatusNotifier.notifySyncStatus(deviceFile, i, str);
    }

    public void updateStatus(long j, int i) {
        if (this.stateMap == null) {
            this.stateMap = new LongSparseArray<>();
        }
        this.stateMap.put(j, i);
    }

    public void notifySyncStatus(DeviceFile deviceFile, int i) {
        if (deviceFile == null) {
            return;
        }
        notifySyncStatus(deviceFile, i, null, true);
    }

    public void notifyProgressUpdate(DeviceFile deviceFile, float f) {
        SyncStatusNotifier syncStatusNotifier = this.notifier;
        if (syncStatusNotifier == null || deviceFile == null) {
            return;
        }
        syncStatusNotifier.notifyProgressUpdate(deviceFile, f);
    }

    public void notifyDeviceFiles(List<DeviceFile> list) {
        SyncStatusNotifier syncStatusNotifier = this.notifier;
        if (syncStatusNotifier == null || list == null) {
            return;
        }
        syncStatusNotifier.notifyReadFileList(list);
    }

    public void notifyDeviceStatus(byte b) {
        SyncStatusNotifier syncStatusNotifier = this.notifier;
        if (syncStatusNotifier != null) {
            syncStatusNotifier.notifyDeviceStatus(b);
        }
    }

    @Override
    public int getSyncState(long j) {
        LongSparseArray<Integer> longSparseArray = this.stateMap;
        Integer num = longSparseArray != null ? longSparseArray.get(j) : null;
        if (num == null) {
            num = (isSynchronised(j) ? DeviceFileStatus.STATUS_SYNCED : DeviceFileStatus.STATUS_NONE).getStatus();
        }
        return num;
    }

    @Override
    public void registerSyncListener(DeviceSyncListener deviceSyncListener) {
        if (this.notifier == null) {
            this.notifier = new SyncStatusNotifier();
        }
        this.notifier.registerSyncListener(deviceSyncListener);
    }

    @Override
    public void unregisterSyncListener(DeviceSyncListener deviceSyncListener) {
        SyncStatusNotifier syncStatusNotifier = this.notifier;
        if (syncStatusNotifier != null) {
            syncStatusNotifier.unregisterSyncListener(deviceSyncListener);
        }
    }

    @Override
    public Object getTag() {
        return this.object;
    }

    @Override
    public void setTag(Object obj) {
        this.object = obj;
    }

    @Override
    public void release() {
        SyncStatusNotifier syncStatusNotifier = this.notifier;
        if (syncStatusNotifier != null) {
            syncStatusNotifier.release();
        }
        LongSparseArray<DeviceFile> longSparseArray = this.deviceFilesMap;
        if (longSparseArray != null) {
            longSparseArray.clear();
        }
        List<DeviceFile> list = this.deviceFiles;
        if (list != null) {
            list.clear();
        }
        LongSparseArray<Integer> longSparseArray2 = this.stateMap;
        if (longSparseArray2 != null) {
            longSparseArray2.clear();
        }
    }

    public static long generateSeq() {
        long j = seq + 1;
        seq = j;
        return j % Long.MAX_VALUE;
    }
}
